home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvipage / args.c < prev    next >
C/C++ Source or Header  |  1992-05-11  |  4KB  |  207 lines

  1. /*
  2.  * dvipage: DVI Previewer Program for Suns
  3.  *
  4.  * Neil Hunt (hunt@spar.slb.com)
  5.  *
  6.  * This program is based, in part, upon the program dvisun,
  7.  * distributed by the UnixTeX group, extensively modified by
  8.  * Neil Hunt at the Schlumberger Palo Alto Research Laboratories
  9.  * of Schlumberger Technologies, Inc.
  10.  *
  11.  * Copyright (c) 1988 Schlumberger Technologies, Inc 1988.
  12.  * Anyone can use this software in any manner they choose,
  13.  * including modification and redistribution, provided they make
  14.  * no charge for it, and these conditions remain unchanged.
  15.  *
  16.  * This program is distributed as is, with all faults (if any), and
  17.  * without any warranty. No author or distributor accepts responsibility
  18.  * to anyone for the consequences of using it, or for whether it serves any
  19.  * particular purpose at all, or any other reason.
  20.  *
  21.  * $Log:    args.c,v $
  22.  * Revision 1.1  88/11/28  18:39:09  hunt
  23.  * Initial revision
  24.  * 
  25.  * Stripped from dvipage.c 1.4.
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <sys/param.h>        /* For MAXPATHLEN */
  30. #include <suntool/sunview.h>
  31. #include "dvipage.h"
  32.  
  33. static char *    a_arg_ptr = NULL;
  34. static int    a_arg_index = 0;
  35. static bool    a_escape_seen;
  36.  
  37. char *        a_prog_name = "Anonymous";
  38.  
  39. /*
  40.  * a_next:
  41.  *    Returns the next flag in the command line,
  42.  *    or A_ARG if it is not a flag,
  43.  *    or A_END if there are no more args.
  44.  */
  45.  
  46. char
  47. a_next(argc, argv)
  48. int argc;
  49. char **argv;
  50. {
  51.     char opt;
  52.  
  53.     /*
  54.      * Checks.
  55.      */
  56.     if(argv == NULL || argc < 1)
  57.     {
  58.         fprintf(stderr, "a_next: bad arguments\n");
  59.         exit(2);
  60.     }
  61.  
  62.     /*
  63.      * Get program name on first call.
  64.      */
  65.     if(a_arg_index == 0)
  66.     {
  67.         a_prog_name = argv[0];
  68.         a_arg_index = 1;
  69.     }
  70.  
  71.     /*
  72.      * If there is part of the previous word left, then return it.
  73.      */
  74.     if(a_arg_ptr && *a_arg_ptr)
  75.         return *a_arg_ptr++;
  76.  
  77.     /*
  78.      * Return A_END after the end of the list.
  79.      */
  80.     if(a_arg_index >= argc)
  81.         return A_END;
  82.  
  83.     /*
  84.      * Look at the next word.
  85.      */
  86.     a_arg_ptr = argv[a_arg_index++];
  87.  
  88.     /*
  89.      * If we have seen the escape "--",
  90.      * or if the first char of the word * is not a '-',
  91.      * or if this is an isolated "-",
  92.      * then return ARG.
  93.      */
  94.     if(a_escape_seen || a_arg_ptr[0] != '-' || a_arg_ptr[1] == '\0')
  95.         return A_ARG;
  96.  
  97.     /*
  98.      * Look at the next char.
  99.      */
  100.     a_arg_ptr++;
  101.     opt = *a_arg_ptr++;
  102.  
  103.     /*
  104.      * If the next char is '-', then this is the escape.
  105.      * start over...
  106.      */
  107.     if(opt == '-')
  108.     {
  109.         a_escape_seen = TRUE;
  110.         return a_next(argc, argv);
  111.     }
  112.  
  113.     /*
  114.      * Otherwise, return this option.
  115.      */
  116.     return opt;
  117. }
  118.  
  119. /*
  120.  * a_arg:
  121.  *    Returns the next argument in the command line,
  122.  *    or NULL if there are no more args.
  123.  */
  124.  
  125. char *
  126. a_arg(argc, argv)
  127. int argc;
  128. char **argv;
  129. {
  130.     char *arg;
  131.  
  132.     /*
  133.      * Checks.
  134.      */
  135.     if(argv == NULL || argc < 1)
  136.     {
  137.         fprintf(stderr, "a_arg: bad arguments\n");
  138.         exit(2);
  139.     }
  140.  
  141.     /*
  142.      * Get program name on first call.
  143.      */
  144.     if(a_arg_index == 0)
  145.     {
  146.         a_prog_name = argv[0];
  147.         a_arg_index = 1;
  148.     }
  149.  
  150.     /*
  151.      * If there is part of the previous word left, then return it.
  152.      */
  153.     if(a_arg_ptr && *a_arg_ptr)
  154.     {
  155.         arg = a_arg_ptr;
  156.         a_arg_ptr = NULL;
  157.         return arg;
  158.     }
  159.  
  160.     /*
  161.      * Return NULL after the end of the list.
  162.      */
  163.     if(a_arg_index >= argc)
  164.         return NULL;
  165.  
  166.     /*
  167.      * Return the next word.
  168.      */
  169.     return argv[a_arg_index++];
  170. }
  171.  
  172. /*
  173.  * a_number:
  174.  *    Interpret the next word or part word as a number.
  175.  */
  176.  
  177. double
  178. a_number(argc, argv)
  179. int argc;
  180. char **argv;
  181. {
  182.     char *arg;
  183.  
  184.     if((arg = a_arg(argc, argv)) == NULL)
  185.         return 0.0;
  186.     else
  187.         return atof(arg);
  188. }
  189.  
  190. /*
  191.  * a_integer:
  192.  *    Interpret the next word or part word as an integer.
  193.  */
  194.  
  195. int
  196. a_integer(argc, argv)
  197. int argc;
  198. char **argv;
  199. {
  200.     char *arg;
  201.  
  202.     if((arg = a_arg(argc, argv)) == NULL)
  203.         return 0;
  204.     else
  205.         return atoi(arg);
  206. }
  207.